﻿using AutoMapper;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SmartConference.Api.Applocations.IServices;
using SmartConference.Api.DTO;
using SmartConference.Domain;
using SmartConference.ErrorCode;
using SmartConference.Infrastructure.Implements;
using SmartConference.Infrastructure.Interfaces;
using System.Transactions;

namespace SmartConference.Api.Controllers
{
    /// <summary>
    /// 权限管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PermissionController : ControllerBase
    {
        private readonly IPermissionServices permissionServices;
        private readonly IRolePermissionServices rolePermissionServices;

        public PermissionController(IPermissionServices permissionServices, IRolePermissionServices rolePermissionServices)
        {
            this.permissionServices = permissionServices;
            this.rolePermissionServices = rolePermissionServices;
        }

        /// <summary>
        /// 获取所有权限
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<GetPermissionDTO>>> GetPermission()
        {
            try
            {
                return await permissionServices.GetPermission();
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 分配权限
        /// </summary>
        /// <param name="RoleId">角色id</param>
        /// <param name="permissionArray">权限数组</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> SetPermission(int RoleId,int[] permissionArray)
        {
            try
            {
                return await rolePermissionServices.SetPermission(RoleId, permissionArray);
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 获取角色权限
        /// </summary>
        /// <param name="RoleId">角色id</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<RolePermission>>> GetRolePermission(int RoleId)
        {
            try
            {
                return await rolePermissionServices.GetRolePermission(RoleId);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
